﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;
using TransCode.Entitys;
using MongoDB.Driver.Builders;
using MongoDB.Bson;

namespace TransCode.Logic
{
    public class VideoLogic
    {
        MongoDatabase db = MongoDbHelper.DbInstance();

        public List<Video> VideoList()
        {
            List<Video> result = new List<Video>();

            result = db.GetCollection<Video>("Video").FindAll().ToList();

            return result;
        }
        public Video Single(string id)
        {
            return db.GetCollection<Video>("Video").FindOneById(id);
        }

        public void Save(Video entity)
        {
            db.GetCollection<Video>("Video").Save(entity);
        }

        public List<Video> Querys(IDictionary<string, object> args)
        {
            // GT 大于    LT 小于 
            var query = Query.And(
                Query.Matches("VideoName", "/" + args["VideoName"].ToString() + "/"),
                Query.GT("EndTime", (DateTime)args["BeginTime"]),
                Query.LT("BeginTime", (DateTime)args["EndTime"]));

            return db.GetCollection<Video>("Video").Find(query).ToList();
        }


        public void Delete(Video entity)
        {
            if (entity != null)
            {
                var query = new QueryDocument("_id", entity.Id);
                db.GetCollection<Video>("Video").Remove(query);
            }
        }
        public void Delete()
        {
            db.GetCollection<Video>("Video").RemoveAll();
        }
    }
}
